/**
 * Project Name:九色鹿
 * File Name:AESUtils.java
 * Package Name:com.fxp.project.jiuselu.onekey.utils.aes
 * Date:2020年04月02日 0:48
 * Copyright (c) 2020, Jason.Wang All Rights Reserved.
 */
package com.fxp.project.jiuselu.onekey.utils.aes;

import com.fxp.project.jiuselu.onekey.utils.bytes.ByteFormat;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * ClassName:AESUtils<br/>
 * Function: AESUtils<br/>
 * Date:2020年04月02日 0:48<br/>
 *
 * @author Jason.Wang
 * @version 1.0.0.0
 * @see
 * @since JDK1.8
 */
public class AESUtils {
    private static final String charset = "UTF-8";

    /**
     * Description: AES解密
     */
    public static String decrypt(String sSrc, String sKey, String siv) throws Exception {
        try {
            if (sSrc == null || sSrc.length() == 0) {
                return null;
            }
            if (sKey == null) {
                throw new Exception("decrypt key is null");
            }
            if (sKey.length() != 16) {
                throw new Exception("decrypt key length error");
            }
            byte[] Decrypt = ByteFormat.hexToBytes(sSrc);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec skeySpec = new SecretKeySpec(sKey.getBytes(charset), "AES");
            //new IvParameterSpec(getIV());
            IvParameterSpec iv = new IvParameterSpec(siv.getBytes(charset));
            //使用解密模式初始化密
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            return new String(cipher.doFinal(Decrypt), charset);
        } catch (Exception ex) {
            throw new Exception("decrypt errot", ex);
        }
    }
}
